package soot.we.android.logic;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import soot.MethodOrMethodContext;
import soot.Pack;
import soot.PackManager;
import soot.Scene;
import soot.SootClass;
import soot.SootMethod;
import soot.Transform;
import soot.Unit;
import soot.Value;
import soot.jimple.IntConstant;
import soot.jimple.InvokeExpr;
import soot.jimple.Stmt;
import soot.we.android.base.ComponentClass;
import soot.we.android.transfomer.ComponentConstructTransformer;

public class MainThread {
	
	
	public static List<ComponentClass> classList = new ArrayList<ComponentClass>();
	private static Set<String> androidCallbacks = new HashSet<String>();
	private static Map<SootClass, Set<Integer>> layoutClasses = new HashMap<SootClass, Set<Integer>>();
	
	public void run() throws IOException {

		androidCallbacks = loadAndroidCallbacks();
		ProcessJimpleClasses procesJimClass = new ProcessJimpleClasses();
		this.contructComponentClass();
	    
		for(int i = 0 ; i < classList.size();i++) {
			  ComponentClass tempCompClass= classList.get(i);
			  procesJimClass.collectCallbackMethod(tempCompClass);
              tempCompClass.setSequence(procesJimClass.createDummySequence(tempCompClass));
              
              TaintedAnalysis taintedanaysis = new TaintedAnalysis();
              taintedanaysis.build(tempCompClass);
		}

	}
	private Set<String> loadAndroidCallbacks() throws IOException {
		Set<String> androidCallbacks = new HashSet<String>();
		BufferedReader rdr = null;
		try {
			rdr = new BufferedReader(new FileReader("AndroidCallbacks.txt"));
			String line;
			while ((line = rdr.readLine()) != null)
				if (!line.isEmpty())
					androidCallbacks.add(line);
		}
		finally {
			if (rdr != null)
				rdr.close();
		}
		return androidCallbacks;
	}
    private void contructComponentClass() {
    	
    	Pack pack1 = PackManager.v().getPack("wjtp");
		pack1.add(new Transform("wjtp.ComponentConstructThrans", new ComponentConstructTransformer()));
    }
    
  
}
